<template>
  <el-drawer v-model="drawerVisible" :z-index="1000" :destroy-on-close="true" :title="`${drawerProps.title}`" size="1000px">
    <div class="gatewayDrawer">
      <div class="tree" v-loading="loading1">
        <ZTree
          :id="treeId"
          :zNodes="drawerProps.zNodes"
          :checkNodeId="drawerProps.row!.mapndx"
          @click-dom="zTreeOnClick"
          v-if="isShow"
        />
      </div>
      <el-form
        class="gatewayForm"
        ref="GatewayFormRef"
        label-width="130px"
        label-suffix=" :"
        :rules="rules"
        :disabled="drawerProps.isView"
        :model="drawerProps.row"
        :hide-required-asterisk="drawerProps.isView"
      >
        <el-form-item label="智能锁位置">
          <el-input v-model="roomMapName" placeholder="请填写锁地图名称" readonly clearable></el-input>
        </el-form-item>
        <el-form-item label="网关名称" prop="gatewayName">
          <el-input v-model="drawerProps.row!.gatewayName" placeholder="请填写网关名称" clearable></el-input>
        </el-form-item>

        <el-form-item label="mac地址">
          <el-input v-model="drawerProps.row!.mac" placeholder="请填写mac地址" clearable></el-input>
        </el-form-item>

        <!-- <el-form-item label="网关状态">
        <el-select v-model="drawerProps.row!.gatewayStatus" placeholder="请选择网关状态" clearable>
          <el-option v-for="item in connectStatus" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item> -->

        <el-form-item label="信道">
          <el-input v-model="drawerProps.row!.channel" placeholder="请填写信道" clearable></el-input>
        </el-form-item>

        <el-form-item label="描述">
          <el-input v-model="drawerProps.row!.description" placeholder="请填写描述" clearable></el-input>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <el-button @click="_colse">取消</el-button>
      <el-button type="primary" :loading="loading" v-show="!drawerProps.isView" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="GatewayDrawer">
/* eslint-disable */
// @ts-nocheck
import { ref, reactive, watch, nextTick } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import ZTree from "@/components/ZTree/index.vue";
import { connectStatus } from "@/utils/serviceDict";
const rules = reactive({
  gatewayName: [{ required: true, trigger: "blur", message: "请填写网关名称" }]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: any;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
  resetTableList?: () => void;
  zNodes: { [key: string]: any }[];
}

const isShow = ref(false);
const treeId = ref("tree1");
const loading = ref(false);
const loading1 = ref(false);
const drawerVisible = ref(false);
const roomMapName = ref("");

const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});
// 接收父组件传过来的参数
const acceptParams = async (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
  loading1.value = true;
  isShow.value = false;
  setTimeout(() => {
    loading1.value = false;
    isShow.value = true;
  }, 500);
};

// 提交数据（新增/编辑）
const GatewayFormRef = ref<FormInstance>();
/**
 * 关闭弹窗
 */
const _colse = () => {
  drawerVisible.value = false;
  GatewayFormRef.value && GatewayFormRef.value.resetFields();
};
/**
 * 提交信息
 */
const handleSubmit = () => {
  GatewayFormRef.value!.validate(async valid => {
    if (!valid) return;
    let params = {
      ...drawerProps.value.row
    };
    if (drawerProps.value.title === "新增") params.gatewayStatus = 0;
    loading.value = true;
    try {
      await drawerProps.value.api!(params);
      ElMessage.success({ message: `${drawerProps.value.title}成功！` });
      drawerProps.value.title === "新增" ? drawerProps.value.resetTableList!() : drawerProps.value.getTableList!();
      drawerVisible.value = false;
      loading.value = false;
    } catch (error) {
      loading.value = false;
      console.log(error);
    }
  });
};

/**
 * 点击树节点
 */
const zTreeOnClick = ({ treeNode }) => {
  drawerProps.value.row.mapndx = treeNode.id;
  roomMapName.value = treeNode.name;
};

defineExpose({
  acceptParams
});
</script>
<style lang="scss" scoped>
.form {
  flex: 1;
  width: 0;
}
.gatewayDrawer {
  display: flex;
  width: calc(100% - 40px);
  height: 100%;
  padding: 0 20px;
  .tree {
    width: 320px;
    height: 100%;
    .ztree {
      height: 100%;
      overflow-y: auto;
    }
  }
  .gatewayForm {
    flex: 1;
    padding: 10px 20px 0;
    overflow-y: auto;
  }
}
</style>
